package rtpsimple

import (
	"fmt"
	"github.com/pion/rtp"
)

// Decoder is a RTP/simple audio decoder.
type Decoder struct {
	expectedSeqNum uint16
}

// Init initializes the decoder.
func (d *Decoder) Init() error {
	d.expectedSeqNum = 0
	return nil
}

// Decode decodes an audio frame from a RTP packet.
func (d *Decoder) Decode(pkt *rtp.Packet) ([]byte, error) {
	if pkt.SequenceNumber != d.expectedSeqNum {
		return nil, fmt.Errorf("invalid sequence number")
	}

	d.expectedSeqNum = pkt.SequenceNumber + 1

	return pkt.Payload, nil
}
